package com.wzh.auth.controller;

import cn.dev33.satoken.secure.BCrypt;
import com.wzh.auth.domain.dto.AuthLoginDTO;
import com.wzh.auth.domain.dto.LoginDTO;
import com.wzh.auth.service.AuthService;
import com.wzh.common.general.log.Log;
import com.wzh.common.general.result.Result;
import com.wzh.user.api.dto.SysUserDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 认证控制器，处理用户登录、注册等认证相关操作
 * </p>
 *
 * @author 山归岿
 * @create 2025-06-15-19:42
 */
@Tag(name = "用户管理")
@Validated
@RestController
@RequestMapping("/")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @GetMapping("/test")
    @Log(module = "用户管理", description = "测试")
    @Operation(summary = "测试")
    public Result<String> test() {
        String hashpw = BCrypt.hashpw("123456");
        boolean checkpw = BCrypt.checkpw("123456", hashpw);
        return Result.success("测试成功");
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    @Log(module = "用户管理", description = "用户登录")
    public Result<LoginDTO> login(@RequestBody AuthLoginDTO authLoginVO) {
        return Result.success(authService.login(authLoginVO));
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/info")
    public Result<SysUserDTO> getCurrentUser() {
        return Result.success(authService.getCurrentUser());
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    @Log(module = "用户管理", description = "退出登录")
    public Result<Void> logout() {
        authService.logout();
        return Result.success();
    }

    @Operation(summary = "修改密码")
    @PutMapping("/password")
    @Log(module = "用户管理", description = "修改密码")
    public Result<Void> updatePassword(
            @Parameter(description = "原密码") @RequestParam String oldPassword,
            @Parameter(description = "新密码") @RequestParam String newPassword) {
        authService.updatePassword(oldPassword, newPassword);
        return Result.success();
    }

    @Operation(summary = "重置密码")
    @PutMapping("/password/{userId}")
    @Log(module = "用户管理", description = "重置密码")
    public Result<Void> resetPassword(
            @Parameter(description = "用户ID") @PathVariable Long userId) {
        authService.resetPassword(userId);
        return Result.success();
    }
}